Deleting an Item in a Singly Linked List
- Just as insertion required $O(n)$ traversal time, deletion carries the same time complexity burden because we must first find the predecessor node at position $i-1$.
- Deleting a node simplifies the operation compared to insertion, requiring only **one** strategic pointer relinking step to bypass the target node.
- The overall time complexity is determined by finding the correct insertion point: $O(n)$ (We traverse up to $i-1$ nodes).
- The space complexity remains $O(1)$ as we only use fixed memory for pointers like $p$.
- The deletion mechanism involves traversing the list using pointer $p$ until $p$ is at position $i-1$ (the predecessor).
- The final step is the relink: update $p$'s `next` pointer to skip the node at position $i$ and point directly to its successor via:
p.next = p.next.next.
Key Deletion Mechanism
By setting p.next = p.next.next, we effectively sever the link to the target node (at index $i$), making it unreachable from the list's head. This node is then eligible for garbage collection, completing the deletion.
| Operation | Complexity | Pointers Used |
|---|---|---|
| Find Predecessor ($i-1$) | $O(n)$ | $p$ |
| Relink/Bypass | $O(1)$ | $p$ |
| Total Deletion Time | $O(n)$ | $O(1)$ |